from pathlib import Path
import shutil
import string
import random

dir_source = '/tmp/test'

if Path(dir_source).exists():
    shutil.rmtree(dir_source)

dir_destination = 'a/b/c/d'
final_path = Path(dir_source).joinpath(dir_destination)
final_path.mkdir(parents=True,exist_ok=True)

#创建文件
p = final_path
while ( p != Path('/tmp/test/')):
    for i in range(50):
        fname = ''.join(random.choices(string.ascii_lowercase,k=4))+ '.txt'
        fpath = p.joinpath(Path(fname))
        fpath.touch()
    p = p.parent

#拷贝文件，自定义fn方法
s1 = '/tmp/test/a'
d1 = '/tmp/copy'

def ignore(src,names):
    ig = filter(lambda x:not (Path(src,x).is_dir() or x.startswith('x') or x.startswith('y') or x.startswith('z')),names)
    return set(ig)

if Path(d1).exists():
    shutil.rmtree('/tmp/copy')

shutil.copytree(s1,d1,ignore=ignore)

# 做的不错
